package com.jeesite.modules.exam.notice.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.service.SysOfficeUnitService;
import com.jeesite.modules.exam.notice.dao.DcSupExamNoticeDao;
import com.jeesite.modules.exam.notice.entity.DcSupExamNoticeOrg;
import com.jeesite.modules.exam.notice.service.DcSupExamNoticeOrgService;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuota;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaService;
import com.jeesite.modules.exam.socreResult.dao.DcSupExamOrgSelectDao;
import com.jeesite.modules.exam.socreResult.entity.DcSupExamOrgSelect;
import com.jeesite.modules.exam.socreResult.service.DcSupExamOrgSelectService;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.utils.OfficeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.exam.notice.entity.DcSupExamNotice;
import com.jeesite.modules.exam.notice.service.DcSupExamNoticeService;

import java.util.ArrayList;
import java.util.List;

/**
 * 考核任务通知表Controller
 * @author xhc
 * @version 2022-05-28
 */
@Controller
@RequestMapping(value = "${adminPath}/notice/dcSupExamNotice")
public class DcSupExamNoticeController extends BaseController {

	@Autowired
	private DcSupExamNoticeService dcSupExamNoticeService;
	@Autowired
	private DcSupExamNoticeOrgService dcSupExamNoticeOrgService;
	@Autowired
	private DcSupExamQuotaService dcSupExamQuotaService;
	@Autowired
	private SysOfficeUnitService sysOfficeUnitService;
	@Autowired
	private DcSupExamOrgSelectService dcSupExamOrgSelectService;
	@Autowired
	private DcSupExamOrgSelectDao dcSupExamOrgSelectDao;
	@Resource
	private DcSupExamNoticeDao noticeDao;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupExamNotice get(String id, boolean isNewRecord) {
		return dcSupExamNoticeService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupExamNotice dcSupExamNotice, Model model) {
		model.addAttribute("dcSupExamNotice", dcSupExamNotice);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		if(StringUtils.inString(dcSupExamNotice.getNoticeType(),"1","2","5")){
			return "modules/exam/notice/dcSupExamNoticeQuotaList";
		}
		return "modules/exam/notice/dcSupExamNoticeList";
	}

	/**
	 * 单项指标评分通知页面
	 */
	@RequestMapping(value = {"dxNoticeView", ""})
	public String dxNoticeView(DcSupExamNotice dcSupExamNotice, Model model) {
		model.addAttribute("dcSupExamNotice", dcSupExamNotice);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		return "modules/exam/notice/dcSupExamDXNoticeList";
	}


	/**
	 * 关键指标评分通知页面
	 */
	@RequestMapping(value = {"gjNoticeView", ""})
	public String gjNoticeView(DcSupExamNotice dcSupExamNotice, Model model) {
		model.addAttribute("dcSupExamNotice", dcSupExamNotice);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		return "modules/exam/notice/dcSupExamGJNoticeList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupExamNotice> listData(DcSupExamNotice dcSupExamNotice, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamNotice.setPage(new Page<>(request, response));
		dcSupExamNotice.setNoticeOrgs(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		//1.首先查询有没有发布的版本 没有联合指标主表 有联合版本指标汇总主表
		Page<DcSupExamNotice> page  = dcSupExamNoticeService.findPage(dcSupExamNotice);
		List<DcSupExamNotice> list = page.getList();
			for(DcSupExamNotice p: list){
				if (StringUtils.equals(p.getNoticeStatus(), "2")) {
					if (StringUtils.equals(p.getNoticeType(), "1")) {
						induceMethod(p);
					}else if(StringUtils.equals(p.getNoticeType(), "2")){
						DcSupExamQuota q = new DcSupExamQuota();
						q.setId(p.getQuotaId());
						DcSupExamQuota dcSupExamQuota = dcSupExamQuotaService.get(q);
						if(StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "2")){
							p.setOperationStatus("sended");
						}else if(StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "1")){
							p.setOperationStatus("send");
						}else if(StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "3")){
							p.setOperationStatus("withdraw");
						}else{
							p.setOperationStatus("pass");
						}
						if(StringUtils.equals(p.getOperationStatus(), "pass")){
							//查询已发布指标汇总表是否存在单项指标版本信息,有才可以有细则补充操作
							Integer count =	dcSupExamQuotaService.findSumQuotaIsDanExist(p.getExamId());
							if(count>=1){
								p.setOperationStatus(dcSupExamQuota.getFileStatus());
							}
						}
					}
				}
			}

		return page;
	}


	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(DcSupExamNotice dcSupExamNotice, Model model) {
		String followFlag =dcSupExamNotice.getNoticeFollow();
		DcSupExamNotice dcSupExamNotice_  = dcSupExamNoticeService.getNoticeByExamId(dcSupExamNotice.getExamId(),dcSupExamNotice.getNoticeType());
		if(dcSupExamNotice_!=null){
			dcSupExamNotice = dcSupExamNotice_;
		}else{
			dcSupExamNotice = dcSupExamNoticeService.initNoticebyExamId(dcSupExamNotice.getExamId(),dcSupExamNotice.getNoticeType(),dcSupExamNotice);
		}
		dcSupExamNotice.setNoticeFollow(followFlag);
		model.addAttribute("dcSupExamNotice", dcSupExamNotice);
		Integer currentYear = Integer.parseInt(DateUtils.getYear());
		model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
		model.addAttribute("currentYear", currentYear);
		if(StringUtils.isNotBlank(dcSupExamNotice.getId())){
			return "modules/exam/notice/dcSupExamNoticeFormDetail";
		}
		return "modules/exam/notice/dcSupExamNoticeForm";
	}

	/**
	 * 保存数据
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupExamNotice dcSupExamNotice) {
		try {
			dcSupExamNoticeService.save(dcSupExamNotice);
			return renderResult(Global.TRUE, text("保存考核任务通知成功！"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}

	
	/**
	 * 停用数据
	 */
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupExamNotice dcSupExamNotice) {
		dcSupExamNotice.setStatus(DcSupExamNotice.STATUS_DISABLE);
		dcSupExamNoticeService.updateStatus(dcSupExamNotice);
		return renderResult(Global.TRUE, text("停用考核任务通知表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupExamNotice dcSupExamNotice) {
		dcSupExamNotice.setStatus(DcSupExamNotice.STATUS_NORMAL);
		dcSupExamNoticeService.updateStatus(dcSupExamNotice);
		return renderResult(Global.TRUE, text("启用考核任务通知表成功"));
	}


	/**
	 * 接收通知
	 */
	@RequestMapping(value = "receiveNotice")
	@ResponseBody
	public String receiveNotice(DcSupExamNoticeOrg scSupExamNoticeOrg) {
		if (!StringUtils.equals(scSupExamNoticeOrg.getNoticeOrg(), sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode())) {
			return renderResult(Global.FALSE, text("接收失败,没有权限"));
		}
		if (StringUtils.inString(scSupExamNoticeOrg.getNoticeType(), "1", "2")){
			DcSupExamNotice noticeDaoList = noticeDao.getNoticeByExamId(scSupExamNoticeOrg.getExamId(), "5");
			if (noticeDaoList != null) {
				return renderResult(Global.FALSE, text("当前任务已发布细则,不允许接收"));
			}
	  }
		long l = dcSupExamNoticeOrgService.updateDcSupExamNoticeOrg(scSupExamNoticeOrg);
		if(l==0){
			return renderResult(Global.FALSE, text("考核任务通知有变动，接收失败，请刷新列表继续操作"));
		}
		if(StringUtils.equals(scSupExamNoticeOrg.getNoticeType(),"1")||
				StringUtils.equals(scSupExamNoticeOrg.getNoticeType(),"2")){
			DcSupExamQuota dcSupExamQuota = new DcSupExamQuota();
			dcSupExamQuota.setExamId(scSupExamNoticeOrg.getExamId());
			dcSupExamQuota.setQuotaGroup(scSupExamNoticeOrg.getNoticeType());
			dcSupExamQuota.setNoticeId(scSupExamNoticeOrg.getNoticeId());
			dcSupExamQuota.setCreateOrg(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
			DcSupExamOrgSelect org = dcSupExamOrgSelectDao.getOrgByName(sysOfficeUnitService.getLatestUnitByOffice().getFullName());
			dcSupExamQuota.setCreateOrgName(org!=null ?org.getOrgName(): sysOfficeUnitService.getLatestUnitByOffice().getFullName());
			List<DcSupExamQuota> dcSupExamQuotas = dcSupExamQuotaService.getExamQuota(dcSupExamQuota);
			for(DcSupExamQuota DcSupExamQuota_:dcSupExamQuotas){
				dcSupExamQuotaService.deleteDcSupExamQuotaId(DcSupExamQuota_.getId());
			}
			dcSupExamQuota.setSubmitStatus("1");
			dcSupExamQuotaService.save(dcSupExamQuota);
		}
		return renderResult(Global.TRUE, text("接收成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("notice:dcSupExamNotice:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupExamNotice dcSupExamNotice) {
		dcSupExamNoticeService.delete(dcSupExamNotice);
		return renderResult(Global.TRUE, text("删除考核任务通知表成功！"));
	}


	@RequestMapping(value = "listQuotaData")
	@ResponseBody
	public Page<DcSupExamNotice> listQuotaData(DcSupExamNotice dcSupExamNotice, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamNotice.setPage(new Page<>(request, response));
		dcSupExamNotice.setNoticeOrgs(sysOfficeUnitService.getLatestUnitByOffice().getOfficeCode());
		//1.首先查询有没有发布的版本 没有联合指标主表 有联合版本指标汇总主表
		//Integer count =	dcSupExamQuotaService.findSumQuotaIsDanExist(dcSupExamNotice.getExamId());
		Page<DcSupExamNotice> page  = dcSupExamNoticeService.findPage(dcSupExamNotice);
		List<DcSupExamNotice> list = page.getList();
		for(DcSupExamNotice p: list){
			if (StringUtils.equals(p.getNoticeStatus(), "2")) {
				if (StringUtils.equals(p.getNoticeType(), "1")) {
					induceMethod(p);
				Integer count  =	dcSupExamNoticeService.findCountKeyList(p.getExamId());
				if(count == 0){
					p.setVersionId(null);
				}
				}else if(StringUtils.equals(p.getNoticeType(), "2")){
					induceMethod(p);
					if(StringUtils.isNotBlank(p.getVersionId())){
							p.setOperationStatus("danVersion");
					}
				}else if(StringUtils.equals(p.getNoticeType(), "5")){
					if(StringUtils.isNotBlank(p.getVersionId())){
						Integer count1 =	dcSupExamNoticeService.findFileStatus1ByVersionId(p.getVersionId(),p.getNoticeOrgs());
						Integer count =	dcSupExamNoticeService.findFileStatusByVersionId(p.getVersionId(),p.getNoticeOrgs());
						if(count1 == 0 && count == 0){
							p.setOperationStatus("auditFileUpload");
						}
						if(count1 >0){
							p.setOperationStatus("pass");
						}
						if(count >0){
							p.setOperationStatus("fileUpload");
						}
					}
				}
			}
		}

		return page;
	}

	private void induceMethod(DcSupExamNotice p) {
		DcSupExamQuota q = new DcSupExamQuota();
		q.setId(p.getQuotaId());
		DcSupExamQuota dcSupExamQuota = dcSupExamQuotaService.get(q);
		if (StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "2")) {
			p.setOperationStatus("sended");
		} else if (StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "1")) {
			p.setOperationStatus("send");
		} else if (StringUtils.equals(dcSupExamQuota.getSubmitStatus(), "3")) {
			p.setOperationStatus("withdraw");
		} else {
			p.setOperationStatus("pass");
		}
	}


	/**
	 * 关键指标评分通知页面
	 */
	@RequestMapping(value = "getSixTeenFiveOrg")
	public String getSixTeenFiveOrg(String ids,String param,Model model) {
		DcSupExamOrgSelect dcSupExamOrgSelect = new DcSupExamOrgSelect();
		Page<DcSupExamOrgSelect> page = new Page<>();
		page.setPageSize(20000);
		dcSupExamOrgSelect.setPage(page);
		page = dcSupExamOrgSelectService.findPage(dcSupExamOrgSelect);
		List<DcSupExamOrgSelect> list = page.getList();
		String [] idss = ids.split(",");
		String idRes = "";
		for (int i = 0; i <list.size() ; i++) {
			for (int j = 0; j < idss.length; j++) {
				if(idss[j].equals(list.get(i).getOrgCode())){
					idRes+=","+list.get(i).getId();
				}
			}
		}
		if(StringUtils.isNotBlank(idRes)){
			idRes = idRes.substring(1);}
		//	model.addAttribute("dcSupExamGroupUnit", idRes);
		model.addAttribute("idRes", idRes);
		model.addAttribute("param", param);
		return "modules/exam/notice/getSixTeenFiveOrg";
	}

	/**
	 * 关键指标评分通知页面
	 */
	@RequestMapping(value = "followSixTeenFiveOrg")
	public String followSixTeenFiveOrg(String ids,String noticeId,String noticeType,String examId,Model model) {
		DcSupExamOrgSelect dcSupExamOrgSelect = new DcSupExamOrgSelect();
		Page<DcSupExamOrgSelect> page = new Page<>();
		page.setPageSize(20000);
		dcSupExamOrgSelect.setPage(page);
		page = dcSupExamOrgSelectService.findPage(dcSupExamOrgSelect);
		List<DcSupExamOrgSelect> list = page.getList();
		String [] idss = ids.split(",");
		String idRes = "";
		for (int i = 0; i <list.size() ; i++) {
			for (int j = 0; j < idss.length; j++) {
				if(idss[j].equals(list.get(i).getOrgCode())){
					idRes+=","+list.get(i).getId();
				}
			}
		}
		if(StringUtils.isNotBlank(idRes)){
			idRes = idRes.substring(1);}
		//	model.addAttribute("dcSupExamGroupUnit", idRes);
		model.addAttribute("idRes", idRes);
		model.addAttribute("noticeId", noticeId);
		model.addAttribute("noticeType", noticeType);
		model.addAttribute("examId", examId);
		return "modules/exam/notice/followSixTeenFiveOrg";
	}


	/**
	 * 追加报送单位
	 */
	@PostMapping(value = "followOrg")
	@ResponseBody
	public String followOrg(DcSupExamNotice notice) {
		try {
			dcSupExamNoticeService.followOrg(notice);
			return renderResult(Global.TRUE, text("追加报送单位成功！"));
		} catch (Exception e) {
			e.printStackTrace();
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
	}

	/**
	 *
	 * @param examId
	 * @return
	 */
	@RequestMapping(value = "isNotSendMessage")
	@ResponseBody
	public String isNotSendMessage(String examId) {
		return dcSupExamNoticeService.isNotSendMessage(examId);
	}
}